# 测试pr
import pygame
import sys
from shape import Shape
from snake import Snake

# 全局定义
SCREEN_X = 600
SCREEN_Y = 600

# 图片
background = pygame.image.load('assets/title.png')
Ico = pygame.image.load('assets/ico.png')
dpad = pygame.image.load('assets/dpad.png')

# 音乐
pygame.mixer.init()
track = pygame.mixer.music.load('assets/snd_bgm.mp3')
# 音效
levelup = pygame.mixer.Sound('assets/snd_win.ogg')
lose = pygame.mixer.Sound('assets/snd_lose.ogg')


def show_text(screen, pos, text, color, font_bold=False, font_size=60, font_italic=False):
    # 获取系统字体，并设置文字大小
    cur_font = pygame.font.SysFont("arial", font_size)
    # 设置是否加粗属性
    cur_font.set_bold(font_bold)
    # 设置是否斜体属性
    cur_font.set_italic(font_italic)
    # 设置文字内容
    text_fmt = cur_font.render(text, 1, color)
    # 绘制文字
    screen.blit(text_fmt, pos)


def door():  # 门户页面
    pygame.init()
    pygame.display.set_caption('Shape of You')
    screen_size = (SCREEN_X, SCREEN_Y)
    screen = pygame.display.set_mode(screen_size)
    mode = 1  # normal 1; easy 0
    ico_x = 120
    ico_y = 330

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_UP and mode == 0:  # 切换至普通难度
                    ico_y = 330
                    mode = 1
                if event.key == pygame.K_DOWN and mode == 1:  # 切换至简单难度
                    mode = 0
                    ico_y = 400
                if event.key == pygame.K_SPACE:
                    run(mode)

        screen.fill((255, 250, 205))
        screen.blit(background, (0, 0))
        screen.blit(Ico, (ico_x, ico_y))
        screen.blit(dpad, (50, 495))
        show_text(screen, (210, 320), 'Normal', (0, 191, 255), False, 50, False)
        show_text(screen, (210, 390), 'Easy', (95, 158, 160), False, 50, False)
        show_text(screen, (110, 465), '>>press SPACE to start<<', (105, 105, 105), False, 30, False)
        show_text(screen, (150, 530), "turn", (112, 128, 144), False, 30)
        show_text(screen, (220, 530), "S speed up", (112, 128, 144), False, 30)
        show_text(screen, (400, 530), "D speed down", (112, 128, 144), False, 30)
        pygame.display.update()


def run(mode):  # 运行页面
    pygame.init()

    pygame.mixer.music.play(-1, 0)
    screen_size = (SCREEN_X, SCREEN_Y)
    screen = pygame.display.set_mode(screen_size)
    pygame.display.set_caption('Snake')
    clock = pygame.time.Clock()
    scores = 0
    isdead = False
    win = False
    lose_played = False  # 是否播放过失败音乐
    level = 1
    leveltime = 0  # 启动提示之前的计时器
    speed = 10
    power = 3
    win_played = False  # 是否播放过胜利音乐
    if mode == 0:  # 简单模式
        power = 9

    # 蛇/图案
    snake = Snake()
    shape = Shape()
    shape.set(level)  # 设置初始关卡

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            if event.type == pygame.KEYDOWN:
                snake.changedirection(event.key)

                # 胜利或失败后按space重新开始游戏
                if event.key == pygame.K_SPACE and isdead:
                    return door()
                elif event.key == pygame.K_SPACE and win:
                    return door()
                # 减速
                elif event.key == pygame.K_s and power > 0:
                    speed /= 2
                    power -= 1
                # 加速
                elif event.key == pygame.K_d:
                    speed *= 2
                    power += 1

        pygame.display.set_caption('Shape of You')

        screen.fill((255, 250, 205))

        level = len(snake.body) - 4

        # 画蛇身
        if (not isdead) and (not win):
            snake.move()

        if not win:
            for rect in snake.body:
                pygame.draw.rect(screen, (255, 99, 71), rect, 0)

        # 图案处理 / 成功+level*20分

        # 图案绘制
        if not win:
            for i in shape.body:
                pygame.draw.rect(screen, (0x66, 0xcc, 0xff), i, 0)

            # 设置提示
            if leveltime >= 30 and mode == 0:
                xtmp = ((leveltime - 30) // 5) % (len(shape.body))
                pygame.draw.circle(screen, (255, 192, 203), (shape.body[xtmp][0] + 12, shape.body[xtmp][1] + 12), 10,
                                   10)
                if xtmp == len(shape.body) - 1:  # 提示了完成一轮
                    leveltime = 15  # 计时器置值

        # 图案的每一块都被蛇契合
        flag = []
        for i in range(len(shape.body)):
            flag.append(0)
        for i in range(len(shape.body)):
            for j in snake.body:
                if shape.body[i] == j:
                    pygame.draw.rect(screen, (0, 255, 127), j, 0)  # 重合位置变色
                    flag[i] = 1  # 图案的第i位被蛇填充
                    break
        delete = 1
        for i in flag:
            if i == 0:  # 存在一位没有被蛇填充
                delete = 0

        # 移除图案，进入下一关
        if delete == 1:
            scores += len(snake.body) * 20
            shape.remove()
            if level != 10:
                levelup.play()  # 播放升级音乐
                leveltime = 0  # 提示计时器清零
                speed = 10  # 速度变回正常
                snake.addnode()
                if level == 8:  # 跳过第9关
                    snake.addnode()
                    shape.set(level + 2)
                else:
                    shape.set(level + 1)  # 设置关卡

            else:  # 游戏通关
                win = True

        # 胜利
        if win == True:
            pygame.mixer.music.stop()
            if win_played == False:
                levelup.play()
                win_played = True
            show_text(screen, (160, 200), 'Victory', (255, 192, 203), False, 100, False)
            show_text(screen, (160, 350), 'press SPACE to restart', (105, 105, 105), False, 30, False)

        # 失败
        isdead = snake.isdead()
        if isdead:
            # 显示失败文字
            show_text(screen, (160, 200), 'Failure', (123, 104, 238), False, 100, False)
            show_text(screen, (160, 350), 'press SPACE to restart', (105, 105, 105), False, 30, False)
            pygame.mixer.music.stop()
            # 播放失败音乐
            if lose_played == False:
                lose.play()
                lose_played = True

        # 显示减速能力数量文字
        show_text(screen, (370, 535), "power:" + str(power), (65, 105, 225), False, 40)

        # 显示倍速文字
        show_text(screen, (10, 10), "speed:" + str(speed / 10.0), (112, 128, 144), False, 30)

        # 显示关卡文字
        show_text(screen, (50, 520), 'level' + str(level), (176, 196, 222))

        # 显示加速减速提示文字
        show_text(screen, (200, 10), "S speed up", (112, 128, 144), False, 30)
        show_text(screen, (370, 10), "D speed down", (112, 128, 144), False, 30)

        leveltime += 1  # 指示计时器

        pygame.display.update()
        clock.tick(speed)


if __name__ == '__main__':
    door()
